package com.cjq.system.controller;

import com.cjq.common.core.utils.poi.ExcelUtil;
import com.cjq.common.core.web.controller.BaseController;
import com.cjq.common.core.web.domain.AjaxResult;
import com.cjq.common.core.web.page.TableDataInfo;
import com.cjq.common.log.annotation.Log;
import com.cjq.common.log.enums.BusinessType;
import com.cjq.common.security.annotation.RequiresPermissions;
import com.cjq.common.security.utils.SecurityUtils;
import com.cjq.system.domain.SysGoods;
import com.cjq.system.service.ISysGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/goods")
public class SysGoodsController extends BaseController
{
    @Autowired
    private ISysGoodsService goodsService;

    /**
     * 获取岗位列表
     */
    @RequiresPermissions("system:goods:list")
    @GetMapping("/list")
    public TableDataInfo list(SysGoods goods)
    {
        startPage();
        List<SysGoods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:goods:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGoods goods)
    {
        List<SysGoods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<SysGoods> util = new ExcelUtil<SysGoods>(SysGoods.class);
        util.exportExcel(response, list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @RequiresPermissions("system:goods:query")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable Long goodsId)
    {
        return success(goodsService.selectGoodsById(goodsId));
    }

    /**
     * 新增岗位
     */
    @RequiresPermissions("system:goods:add")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysGoods goods)
    {
        if (!goodsService.checkGoodsNameUnique(goods))
        {
            return error("新增岗位'" + goods.getGoodsName() + "'失败，岗位名称已存在");
        }
        else if (!goodsService.checkGoodsCodeUnique(goods))
        {
            return error("新增岗位'" + goods.getGoodsName() + "'失败，岗位编码已存在");
        }
        goods.setCreateBy(SecurityUtils.getUsername());
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改岗位
     */
    @RequiresPermissions("system:goods:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysGoods goods)
    {
        if (!goodsService.checkGoodsNameUnique(goods))
        {
            return error("修改岗位'" + goods.getGoodsName() + "'失败，岗位名称已存在");
        }
        else if (!goodsService.checkGoodsCodeUnique(goods))
        {
            return error("修改岗位'" + goods.getGoodsName() + "'失败，岗位编码已存在");
        }
        goods.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除岗位
     */
    @RequiresPermissions("system:goods:remove")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(goodsService.deleteGoodsByIds(goodsIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysGoods> goodss = goodsService.selectGoodsAll();
        return success(goodss);
    }
}
